package cn.wolfcode.shop.consumer;

import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.util.ActiveMQConstants;
import cn.wolfcode.shop.vo.OrderVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

/**
 * 生产订单队列消息的消费者
 */
@Component
public class OrderConsumer {
    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 监听消息队列信息
     */
    @JmsListener(destination = ActiveMQConstants.ORDER_DESTINATION)
    public void listernQueueOrderMsg(Message message) throws JMSException{
        TextMessage textMessage=(TextMessage)message;
        String orderJson=textMessage.getText();
        OrderVo orderVo = JSON.parseObject(orderJson, OrderVo.class);
        //调用生成订单的方法
        orderInfoService.createOrder(orderVo);
    }
}
